﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QuantumQuest
{
    public partial class Page : UserControl
    {
        /*private TransformGroup _transGroup;
        private RotateTransform _rotateTrans;
        private TranslateTransform _translateTrans;*/


        
        private QQHUD _hud;
        private DispatcherTimer _gameLoopTimer;
        private int _fps = 0;
        private DateTime _lastFps = DateTime.Now;
        private QQList _levels;
        private bool _isDone;
       
        private bool _firstInit;

        private int _levelIndex;

        private QQTestLevel1 _level1;
        private QQTestLevel2 _level2;
        private QQTestLevel3 _level3;
         

        public Page()
        {
            

            InitializeComponent();
            InitializeGameComponents();
            
            InitializeLevels();
            InitializeGameLoop();


            /*Ellipse el = new Ellipse();
            
            SolidColorBrush scb = new SolidColorBrush();
            scb.Color = Colors.Blue;
            el.Fill = scb;
            el.Height= 100;
            el.Width = 100;
            PageCanvas.Children.Add(el);*/
            
            Begin();
            CheckGameRunning();
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
            this.KeyUp += new KeyEventHandler(Page_KeyUp);
        }

        void Page_KeyUp(object sender, KeyEventArgs e)
        {
            while (QQGlobal.KeysDown.Contains(e.Key))
            {
                QQGlobal.KeysDown.Remove(e.Key);
            }
        }

        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (!QQGlobal.KeysDown.Contains(e.Key))
            {
                QQGlobal.KeysDown.Add(e.Key);
            }
            
        }
        private void InitializeGameComponents()
        {
           
           
            _firstInit = true;
            

            _hud = new QQHUD();
            PageCanvas.Children.Add(_hud);
            _hud.SetValue(Canvas.ZIndexProperty, QQGlobal.HUDZ);

            _levels = new QQList(20);
            _isDone = false;
            
            _levelIndex = 0;

        }
        private void CheckGameRunning()
        {
            if (!IsDone)
            {
               
               
            }
            else
            {
              
                End();
            }
           
           
        } 
        public void Begin()
        {
            _gameLoopTimer.Start();
        }
        private void End()
        {


        }
        public bool IsDone
        {
            get
            {
                return _isDone;
            }

        }
        
        private void GetFps()
        {
            _fps++;
            if ((DateTime.Now - _lastFps).Seconds >= 1)
            {
                _hud.FPSText.Text = Convert.ToString(_fps);
                _fps = 0;
                _lastFps = DateTime.Now;
                
            }
        }
        private void InitializeGameLoop()
        {
            _gameLoopTimer = new DispatcherTimer();
            _gameLoopTimer.Interval = TimeSpan.FromSeconds((QQGlobal.FramerateSeconds));
            _gameLoopTimer.Tick += new EventHandler(_gameLoopTimer_Tick);
            //_gameLoopTimer.Start();
        }
        public void GameIsDone()
        {
            _isDone = true;
            CheckGameRunning();
        }
        private void UpdateHUD()
        {
            //_score += ((QQLevel)_levels[_levelIndex]).Score;//bad, runs to infinity
            _hud.LevelText.Text = Convert.ToString(_levelIndex+1);
            _hud.ScoreText.Text = Convert.ToString(QQGlobal.Score);
            _hud.SetValue(Canvas.ZIndexProperty, QQGlobal.HUDZ);
        }
        void _gameLoopTimer_Tick(object sender, EventArgs e)
        {
            GetFps();

            UpdateHUD();
            


            if (_firstInit)
            {
                PageCanvas.Children.Add((QQLevel)_levels[_levelIndex]);
                //PageCanvas.Children[_levelIndex].SetValue(Canvas.ZIndexProperty, QQGlobal.LevelZ);
                ((QQLevel)_levels[_levelIndex]).Begin();
                _firstInit = false;
                
            }

            if (_levelIndex < _levels.Count)
            {
                if (((QQLevel)_levels[_levelIndex]).IsFinished)
                {
                    ((QQLevel)_levels[_levelIndex]).ShutDown();
                    PageCanvas.Children.Remove(((QQLevel)_levels[_levelIndex]));
                    //_levels.RemoveAt(_levelIndex);
                    if (_levelIndex + 1 < _levels.Count)
                    {
                        
                        _levelIndex++;
                        PageCanvas.Children.Add((QQLevel)_levels[_levelIndex]);
                        //PageCanvas.Children[_levelIndex].SetValue(Canvas.ZIndexProperty, QQGlobal.LevelZ);
                        ((QQLevel)_levels[_levelIndex]).Begin();
                    }
                    else
                    {
                        GameIsDone();
                    }

                }

            }
            else
            {
                GameIsDone();
            }

            /*for (int i = 0; i < _levels.Count; i++)
            {
                PageCanvas.Children.Add((QQLevel)_levels[i]);
                while (!((QQLevel)_levels[i]).IsFinished)
                {

                    ((QQLevel)_levels[i]).Step();
                }
                
            }*/

        }
        private void InitializeLevels()
        {
            _levels = new QQList(QQGlobal.NumLevels);
            _level1 = new QQTestLevel1();
            _level2 = new QQTestLevel2();
            _level3 = new QQTestLevel3();

            _level1.SetValue(Canvas.LeftProperty, 100.0);
            _level1.SetValue(Canvas.TopProperty, 50.0);

            _level2.SetValue(Canvas.LeftProperty, 100.0);
            _level2.SetValue(Canvas.TopProperty, 50.0);

            _level3.SetValue(Canvas.LeftProperty, 100.0);
            _level3.SetValue(Canvas.TopProperty, 50.0);


            //_levels.Add(_level1);
            //_levels.Add(_level2);
            _levels.Add(_level3);
            

            for (int i = 0; i < _levels.Count; i++)
            {
                ((QQLevel)_levels[i]).IsFinished = false;
            }

            



        }
    }
}
